package com.aiss.ws.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiss.ws.Entity.ExamItem;
import com.aiss.ws.R;
import com.aiss.ws.adapter.AnswerSucessAdatper;
import com.aiss.ws.adapter.ChangeTitleAdatper;
import com.aiss.ws.utils.Command;
import com.aiss.ws.utils.Futil;
import com.aiss.ws.utils.Url;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by chenqiang on 16/5/17.
 */
@EActivity(R.layout.activity_changetitle)
public class ChangeTitleActivity extends BaseActivity {

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if(msg.what==1){
                Intent intent = new Intent();
                intent.putExtra("position",msg.arg1);
                setResult(2,intent);
                finish();
            }

        }
    };

    private Context context  = this;

    private int num = 0;

    private StringBuilder sb = new StringBuilder();

    /**
     * 分数
     */
    @ViewById(R.id.fraction_tv)
    TextView fraction_tv;

    /**
     * 分数布局
     */
    @ViewById(R.id.fraction_ll)
    LinearLayout fraction_ll;


    @ViewById(R.id.title)
    TextView title_tv;

    @ViewById(R.id.next)
    TextView next_tv;


    @Extra
    ExamItem item;

    @Extra
    String idx;

    private ChangeTitleAdatper adapter;

    private AnswerSucessAdatper adatper2;


    @Click(R.id.back)
    void setBack(){
        finish();
    }

    @ViewById(R.id.gridView)
    GridView gridView;

    @Extra()
    int type;

    @AfterViews
    void init() {
        next_tv.setVisibility(View.VISIBLE);
        if (item.getTestItems() != null)
            next_tv.setText(item.getTestItems().size() + "");
        title_tv.setText("选题");
        if (Command.SUITE[1] == type) {
            adapter = new ChangeTitleAdatper(item.getTestItems(), this, handler);
            gridView.setAdapter(adapter);
        } else {

            for (int position = 0; position < item.getTestItems().size(); position++) {
                if (item.getTestItems().get(position).getType().equals("单选题") || item.getTestItems().get(position).getType().equals("判断题")) {
                    if (item.getTestItems().get(position).getMe_answer() != null) {
                        if (Command.ANSWER[Integer.parseInt(item.getTestItems().get(position).getMe_answer())].equals(item.getTestItems().get(position).getAnswer())) {
                            ++num;
                        } else {
                            sb.append(item.getTestItems().get(position).getIdx() + ",");
                        }
                    } else {
                        sb.append(item.getTestItems().get(position).getIdx() + ",");
                    }


                } else {//多选题

                    if (item.getTestItems().get(position).getMe_more_answer() != null && item.getTestItems().get(position).getMe_more_answer().size() != 0) {
                        String xn = "";
                        for (int i = 0; i < item.getTestItems().get(position).getMe_more_answer().size(); i++) {
                            xn += Command.ANSWER[Integer.parseInt(item.getTestItems().get(position).getMe_more_answer().get(i))];
                        }

                        if (xn.equals(item.getTestItems().get(position).getAnswer())) {
                            ++num;
                        } else {
                            sb.append(item.getTestItems().get(position).getIdx() + ",");
                        }


                    } else {
                        sb.append(item.getTestItems().get(position).getIdx() + ",");
                    }

                }
            }

            adatper2 = new AnswerSucessAdatper(item.getTestItems(), this, handler);
            gridView.setAdapter(adatper2);
        }

    }


}
